/**
 * 微信蓝牙手环主页界面
 */
var Mod_Start_Page = Class.create(Common, WeixinSignature, Mod_Draw, {
	initialize: function() {
		this.isTouched = false;
		this.startpoint = {x: 0, y: 0};
		this.endpoint = {x: 0, y: 0};
		this.topValue = 0;
		
		this.loadIndexSleepDataString = "";
		// 首页界面数据
		this.modData = {};
		this.message_height=0;
		this.logo_height=0;
	},
	
	initAchievement: function(){
		console.log("initAchievement");		
		if (this.modData.indexAchievementData.imageUrl) {
			var headImgUrl=this.modData.indexAchievementData.imageUrl;
			headImgUrl=headImgUrl.substring(0, (headImgUrl.length-1));
			headImgUrl+="64";
			this.getElement("user_ico_header").src = headImgUrl;//头像
		}
		this.getElement("user_name").innerHTML=this.modData.indexAchievementData.name;//名字
		this.getElement("achieve").style.display = "block";
		this.getElement("load").style.display = "block";
	},

//	initTouchEvent: function(){
//		var ibody = this.getElement("main");
//		ibody.style.top="0px";
//		ibody.addEventListener("touchmove", this._touchmove, false);
//		ibody.addEventListener("touchstart", this._touchstart, false);
//		ibody.addEventListener("touchend", this._touchend, false);
//	},
//	_touchmove: function(ev){
//		var ibody=this.getElement("main");
//		event = ev || window.event;  
//	    var point = {x:0,y:0};  
//	    point.x = event.touches[0].clientX;
//	    point.y = event.touches[0].clientY;
//
//		var scrollHeight=point.y - this.startpoint.y;
//		if(getScrollTop()==0&&scrollHeight>0&&this.isTouched==true){
//			var ajaxLoader=this.getElement("ajaxLoader");
//			if(this.getScrollTop()==0&&scrollHeight>120){
//				ajaxLoader.style.display="block";
//			} else {
//				ajaxLoader.style.display="none";
//			}
//			ev.preventDefault();
//			scrollHeight=scrollHeight/3;
//			this.topValue=scrollHeight;
//			ibody.style.top=scrollHeight+"px";
//		}else{
//			this.isTouched=false;
//		}
//	},
//
//	_touchstart: function(ev){
//		var ibody=this.getElement("main");
//		ibody.style.webkitTransition="none";
//		var ajaxLoader=this.getElement("ajaxLoader");
//		ajaxLoader.style.display="none";
//
//		if(this.getScrollTop()==0&&this.topValue==0){
//			this.isTouched=true;
//			event = ev || window.event;  
//			startpoint.x = event.touches[0].clientX;
//			startpoint.y = event.touches[0].clientY;
//		} else {
//			this.isTouched=false;
//		}
//	},
//
//	_touchend: function(ev){
//		var ibody=this.getElement("main");
//		if(this.getScrollTop()==0){
//			event = ev || window.event;  
//			this.endpoint.x = event.changedTouches[0].clientX;
//			this.endpoint.y = event.changedTouches[0].clientY;
//		
//			if(topValue>40){
//				setTimeout(clickRefresh, 1000);
//				ibody.style.top ="45px";
//				ibody.style.webkitTransition = "all 0.3s ease 0.1s";
//			}else{
//				ibody.style.top ="0px";
//				ibody.style.webkitTransition = "all 0.3s ease 0.1s";
//			}
//			this.topValue=0;
//	},
	
	getScrollTop: function(){
		var scrollTop = 0;
		if (this.getPlatform()=='ios') {
			if (document.documentElement
					&& document.documentElement.scrollTop) {
				scrollTop = document.documentElement.scrollTop;
			} else if (document.body) {
				scrollTop = document.body.scrollTop;
			}
		} else {
			if (document.documentElement && document.documentElement.scrollTop) {
				scrollTop = document.documentElement.scrollTop;
			} else if (document.body) {
				scrollTop = document.body.scrollTop;
			}
		}

		return scrollTop;
	},

	getPlatForm: function(){
		var name = navigator.platform;	
		if (name.substring(0, 6) == "iPhone" || name.substring(0, 4) == "iPod"
				|| name.substring(0, 4) == "iPad")
				return 'ios';
		else
			return 'android';
	},
	
	initClickEvent: function() {
		this.getElement("message-close").addEventListener("click", this.closeMessage, false);
		this.getElement("user_info").addEventListener("click", this.clickUserAchievements, false);
		this.getElement("user_setting").addEventListener("click", this.clickSetting, false);
	},

	clickUserAchievements: function(){
		document.title = "我的成就";
		var memberId=this.getElement("memberId").value;
		var openId=this.getElement("openId").value;
		window.location.href="/page/achivement?memberId="+memberId+"&openId="+openId;
	},

	//显示页头消息
	showMessage: function(message){
		var obj = this.getElement("message");
		var contentObj = this.getElement("message-content");

		if (obj != null){
			obj.style.display = "block";
			this.message_height=55;		
		}

		if (contentObj != null){
			contentObj.innerHTML = message;
		}
	},
	//关闭页头消息
	closeMessage: function(){
		var obj = this.getElement("message");
		if (obj != null){
			obj.style.display = "none";
			this.message_height=0;
		}
	},

	clickSetting: function(){
		var userType=Common.getElement("userType").value;
		var enterpriseId=Common.getElement("enterpriseId").value;
		var openId=Common.getElement("openId").value;
		var memberId=Common.getElement("memberId").value;
		
		window.location.href="/page/setting?userType="+userType+"&&enterpriseId="+enterpriseId+"&&openId="+openId+"&&memberId="+memberId;
	},
	
	ajaxLoadIndexData: function(){		
		var unionId = this.getElement("unionId").value,
				openId = this.getElement("openId").value,
				accountId = this.getElement("accountId").value,
				memberId = this.getElement("memberId").value,
				enterpriseId = this.getElement("enterpriseId").value,					
				imgUrl = "",
				linkUrl = "",
				ajaxUrl = "/api/ajax/getDefaultData?openId="+openId+"&accountId="+accountId+"&memberId="+memberId+"&mathNum="+Math.random()+"&loadIndexDataString="+this.loadIndexSleepDataString+"&enterpriseId="+enterpriseId;

		this.ajaxRequest(ajaxUrl, this.ajaxLoadIndexDataCallback, this);
	},
	
	ajaxLoadIndexDataCallback: function(response) {
		this.modData = response;
		this.getElement("comIcon_div").style.display="none";
		this.getElement("toolbar").style.display="none";		
		
		var userType = this.getElement("userType").value;
		console.log("userType "+userType);
		
		var ibody=this.getElement("content_div");
		ibody.style.display="block";
		this.getElement("isFirstUse").value = this.modData.isFirstUse;		
		
		
		this.initAchievement();
		
		console.log("why am I disappear?");
		// 加载运动数据
		if (this.modData.indexPedometerData)
			this.initPedometer();
		
		// 加载挑战模块
		if (this.modData.indexDekaronData)
			this.initDekaraon();
	
		// 加载睡眠数据
		this.getElement("countDevicePed").value = jsonData.countDevicePed;
		this.getElement("deviceBindingState").value = jsonData.deviceBindingState;		
		this.ajaxLoadIndexSleepData();
	},
	
	// 挑战
	initDekaraon: function() {
		console.log("initDekaraon");
		
	},
	
	getAndroidUA: function (){
		var uaName = navigator.userAgent;
		return uaName.substring(uaName.indexOf("; Android") - 1, uaName.indexOf("; Android"));
	},
	
	initLayout: function() { 
		console.log("initLayout");
		var hidden_height=0;
		this.loadIndexSleepDataString="";
		var modTotalHeight = this.modData.memberFunctionList.length*150;
		
//		for (var i = 0; i < this.modData.memberFunctionList.length; i++) {
//			var m = this.getElement(this.modData.memberFunctionList[i].url);
//			if (this.modData.memberFunctionList[i].open==1) {
//				this.loadIndexSleepDataString+=this.modData.memberFunctionList[i].name;
//				m.style.display="block";
//				m.style.position="absolute";
//				m.style.top=(this.modData.memberFunctionList[i].sequence-1)*150+90+this.message_height-hidden_height+"px";
//			} else {
//				m.style.display="none";
//				hidden_height+=150;
//			}
//		}
		
		var container=this.getElement("container");
		var modHeight=0;
		if (this.getAndroidUA() == "U") {
			modHeight = modTotalHeight+90-hidden_height;
			container.style.height=modHeight-(90-this.logo_height)+"px";
		} else {
			modHeight = modTotalHeight+90-hidden_height;
			container.style.height=modHeight-(90-this.logo_height)+"px";
		}
		
		var toolbar=this.getElement("toolbar");	
		
		if (this.getPlatform() == 'ios') {
			toolbar.style.top=modHeight+10+"px";
		} else {
			toolbar.style.top=modHeight+10+"px";
		}
	},
	
	initPedometer: function(){
		console.log("initPedometer");
		var name = navigator.platform;
		var deviceBindingState=this.modData.indexPedometerData.deviceBindingState;
		this.getElement("pedometer_upload").style.display="none";
		
//		if (this.getPlatform()=='ios') {
//			init_piphone_onclick();
//		} else {
//			init_phtml_onclick();
//		}
		console.log("deviceBindingState "+deviceBindingState);
		if (deviceBindingState!=1) {
			if (this.modData.indexPedometerData.stepDay==0) {
				this.getElement("pedometer_circle_havedate").style.display="none";
				this.getElement("pedometer_circle_havetext").style.display="none";
				this.getElement("pedometer_circle_text").style.display="none";
				this.getElement("pedometer_circle_step").style.display="none";
				this.getElement("pedometer_today").style.display="none";
				this.getElement("pedometer_circle_notext").style.display="block";
			} else {
				this.getElement("pedometer_circle_havedate_value").innerHTML=this.modData.indexPedometerData.dateDay;
				this.getElement("pedometer_circle_havedate").style.display="block";
				this.getElement("pedometer_circle_havetext").style.display="block";
				this.getElement("pedometer_circle_step").innerHTML=this.modData.indexPedometerData.stepDay;
				this.getElement("pedometer_today_per").innerHTML=this.modData.indexPedometerData.stepPre;
			}
			if (this.modData.indexPedometerData.weekPre=="0%") {
				this.getElement("pedometer_week").style.display="none";
			} else {
				this.getElement("pedometer_week_per").innerHTML=this.modData.indexPedometerData.weekPre;
			}
			//testDrawTree2();
		}else{
			this.getElement("pedometer").style.display="none";
			this.getElement("pedometer_no").style.display="block";
		}
	},
	
	//绘制睡眠mod页面初始数据
	initDrawSleepData: function(){
		var data = {
			    "checkVersion": 0,
			    "countDevicePed": 1,
			    "deviceBindingState": 3,
			    "hLine": 0,
			    "nullTips": "无睡眠记录",
			    "sleepHoursM": 0,
			    "sleepQuality": "",
			    "weekSleep": 0,
			    "xLables": "一,二,三,四,五,六,日",
			    "yValues": ""
			};
		
		var canvas = this.getElement("mod-sleep-canvas");
		var treeTW = this.getElement("mod-sleep-rTree").offsetWidth;
		//默认数据
		this.drawTreeToHomeSleep(treeTW * 2,75 * 2,canvas,"#f7882d","#f7882d","gray",data);
	},
	
	ajaxLoadIndexSleepData: function(){
		console.log("ajaxLoadIndexSleepData");
		this.showLoader();		
		this.initDrawSleepData();
						
		var openId=this.getElement("openId").value;
		var memberId = this.getElement("memberId").value;
		var url = "/api/ajax/getSleepData?openId=" + openId + "&memberId=" + memberId + "&clear=" + new Date() * 1;		
		this.ajaxRequest(url, this.initSleepMod, this);
	},

	initSleepMod: function(indexSleepData) {
		var deviceBindingState=indexSleepData.deviceBindingState;
		if (deviceBindingState==1) {
			this.getElement("sleep").style.display="none";
			this.getElement("sleep_no").style.display="block";
		}
	
		//this.getElement("mod-sleep").addEventListener("click", mod_sleep_goSleepPage, false);
		
		var data = indexSleepData;
		var daySleepM = data.sleepHoursM;
		var weekSleepM = data.weekSleep;
		
		var sleepQuality = data.sleepQuality;
		if ((daySleepM / 60 + "").split(".").length > 1) {
			daySleepM = (daySleepM / 60 + "").split(".")[0] + "小时" + daySleepM % 60 + "分钟";
		} else {
			daySleepM = daySleepM > 0 ? daySleepM / 60 + "小时 0分钟" : "0小时";
		}
		
		if (weekSleepM) {
			if ((weekSleepM / 60 + "").split(".").length > 1) {
				this.getElement("mod-sleep-rTreeText").style.display = "block";
				weekSleepM = (weekSleepM / 60 + "").split(".")[0] + "小时" + weekSleepM % 60 + "分钟";	
			} else {
				weekSleepM = (weekSleepM / 60 + "").split(".")[0] + "小时";
			}			
		} else {
			this.getElement("mod-sleep-rTreeText").style.display = "none";
		}
		
		if (sleepQuality.length == 0) {
			sleepQuality = "无";	
			this.getElement("mod-sleep-dayQuality").style.color = "gray";
		} else {
			this.getElement("mod-sleep-dayQuality").style.color = "#f7882d";
		}
	
		this.getElement("mod-sleep-dayHours").innerHTML = daySleepM;
		this.getElement("mod-sleep-dayQuality").innerHTML = sleepQuality;
		this.getElement("mod-sleep-weekHours").innerHTML = weekSleepM;
				
		var canvas = this.getElement("mod-sleep-canvas");
		var treeTW = this.getElement("mod-sleep-rTree").offsetWidth;
		
		if (this.modData.indexSleepData.weekSleep) 
			this.drawTreeToHomeSleep(treeTW * 2,75 * 2,canvas,"#f7882d","#f7882d","gray",indexSleepData);
		
		this.hiddenLoader();
	},
	
	main: function(){
		this.checkSignature();
		this.initClickEvent();
		
		this.ajaxLoadIndexData();
	}
	
});
